package com.wngbms.netty.seatdemo.service;

import com.wngbms.netty.seatdemo.comm.CmdType;
import com.wngbms.netty.seatdemo.message.SeatInMessage;
import com.wngbms.netty.seatdemo.server.SeatBizHandler;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

@Slf4j
public class ConnectHandler implements SeatTypeHandler {
    public static final CmdType CMD_TYPE = CmdType.CONNECT;

    @Override
    public void handleInbound(SeatInMessage seatInMessage) {
        byte[] body = seatInMessage.getBody();
        String userId = new String(body, StandardCharsets.UTF_8);
        log.info("用户建立连接:{}", userId);

        Channel channel = seatInMessage.getChannelHandlerContext().channel();
        SeatBizHandler.USER_CHANNEL_MAP
                .put(userId, channel);
        SeatBizHandler.USER_SOCKET_MAP.put(channel.remoteAddress(), userId);

        sleep(1000);
        HandleFactory.getHandler(CmdType.EVENT).handleOutbound(userId);
        sleep(1000);
        HandleFactory.getHandler(CmdType.EXTRA_INFO).handleOutbound(userId);
    }


}
